package com.lyf.scm.plan.api.controller;

import com.github.pagehelper.PageInfo;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.model.ExcelExportDataDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.lyf.scm.plan.api.dto.DemandPlanDTO;
import com.lyf.scm.plan.api.dto.DemandPlanInfoDTO;
import com.lyf.scm.plan.domain.service.DemandPlanService;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * description:
 *          需求计划
 * @author: haodong
 * @data: 2020/8/7 16:27
 */
@Slf4j
@Api(tags = "需求计划")
@RequestMapping("/api/v1/demandPlan")
@RomeController
public class DemandPlanController {

    @Resource
    private DemandPlanService demandPlanService;

    @PostMapping("/listByWeek")
    @ApiOperation(value = "按条件查询需求计划")
    public Response<PageInfo<DemandPlanInfoDTO>> listWeekBySelective(@RequestBody DemandPlanDTO demandPlan) {
        return Response.builderSuccess(demandPlanService.listWeekBySelective(demandPlan));
    }

    @PostMapping("/listAll")
    @ApiOperation(value = "查询需求范围指标(范围)")
    public Response<List<DemandPlanInfoDTO>> listAllByCycles(@RequestBody DemandPlanDTO demandPlan){
        return Response.builderSuccess(demandPlanService.listAllByCycles(demandPlan));
    }

    @PutMapping("/tidyData")
    @ApiOperation(value = "定时任务整合需求计划数据的接口")
    public Response tidyData(){
        return demandPlanService.tidyData()>0?Response.builderSuccess(ResponseMsg.SUCCESS.getMsg()):
                Response.builderFail(ResponseMsg.FAIL.getCode(), ResponseMsg.FAIL.getMsg());
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void exportExcel(@RequestBody DemandPlanDTO demandPlanDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse){
        try{
            ExcelExportDataDTO excelExportDataDTO = demandPlanService.exportExcel(demandPlanDTO, type);
            ExcelExportUtil.exportExcel(httpServletResponse, excelExportDataDTO);
        }catch (Exception e){
            System.out.println(e.toString());
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除对应标识数据")
    public Response delete(@RequestParam("opSign") String opSign) {
        demandPlanService.delete(opSign);
        return Response.builderSuccess(null);
    }
}
